/** layui-v2.5.7 自定义模块 左侧菜单渲染 */
layui.define(['jquery','layer','laytpl'],function(exports){
	//使用内置模块
	var $ = layui.jquery,layer = layui.layer,laytpl = layui.laytpl;
	var ulEl;
	var obj = function(){
		this.setting = {
			el:''
			,data:[],
			,url:''
			,fields:{name:'name',icon:'icon',url:'url',path:'path',tips:'tips',sort:'sort',index:'index'}
			,elClick:function(item,el){ return false;}
			,sendCall:function(req){return req.['data'];}
		}
	};
	var load_ = function(data){
		var index = 0,lv = 1,sort = 0;
		var fs = obj.setting.fields;
		//内层递归处理
		var recursion = function(){
			
		}
		//第一层循环
		for(var i = 0,l = data.length ,item; i < l ; i++ ){
			index ++;
			item = data[i];
			item[fs.index] = index;
			item[fs.level] = lv;
			item[fs.sort] = i;
		}
	}
	var select_ = function(e){
		
	}
	var init_ = function(conf){
		var setting = obj.setting;
		if('object' == typeof conf){
			for(var c in conf){
				if( c === 'fields'){
					for(var f in conf.fields){ obj.setting.fields[f] = conf.fields[f];}
				}else{
					obj.setting[c]=conf[c];
				}
			}
			obj['el'] = $(obj.setting.el);
		}else if('string' == typeof conf){
			obj['el'] = $(conf);
		}
		if(obj['el'].length != 1){
			console.error("The element found is not a",conf);return;
		}
		var div = $('<div class="layui-side-scroll"></div>');
		ulEl = $('<ul class="layui-nav layui-nav-tree" lay-shrink="all" lay-filter="layadmin-system-side-menu"></ul>')
		obj['el'].append(div.append(ulEl));
		if('string' == typeof setting.url && setting.url.length > 0){
			$.post(setting.url,function(r){
				setting.data = setting.sendCall(r);
				obj.load(setting.data);
			})
		}else if(setting.data){
			obj.load(setting.data);
		}else{
			console.error("Unknown loading mode",setting);
		}
	}
	obj.prototype.select = function(e){
		select_(e)
	};
	obj.prototype.load = function(data){
		if($.isArray(data) && setting.data.length > 0){
			load_(data);
		}
	};
	obj.prototype.init = function(conf){
		init_(conf);
	};
	//输出组件接口
	exports('menuTree',obj);
})